<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">

      <globalSearchTemplate :config="globalConfig" v-model="listQuery.legalPerson"
                            placeholder="请输入姓名"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.boundUserId" placeholder="请选择用户" prop="boundUserId"
                            type="select" :load="userRemoteDataUrl"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.businessType" placeholder="请选择商家类型" prop="businessType"
                            type="select" ></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.mobile"
                            placeholder="请输入手机号"></globalSearchTemplate>
      <el-button class="filter-item" v-waves @click="handleSearch" icon="el-icon-search">搜索</el-button>
      <el-button
        class="filter-item"
        v-if="businessManager_btn_add"
        v-waves
        @click="handleCreate"
        type="primary"
        icon="edit"
      >添加
      </el-button>
    </div>
    <el-table
      :key="tableKey" :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%">
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="编号"
                         prop="serialNumber" width="120"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="所属用户"
                         prop="boundUserId" type="select" :load="userRemoteDataUrl" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="商家类型"
                         prop="businessType" type="select" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="商家手机"
                         prop="mobile" width="120"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="商家姓名"
                         prop="legalPerson" width="120"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="身份证号"
                         prop="idCard" width="170"></globalTableColumn>
      <el-table-column label="银行信息" width="220" align="center">
        <template slot-scope="scope">
          <div>
            <div>{{'所属银行: ' + scope.row.bankName}}</div>
            <div>{{'银行账号: ' + scope.row.bankAccount}}</div>
          </div>
        </template>
      </el-table-column>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="纳税人识别号"
                         prop="identificationNumber" width="150"></globalTableColumn>

      <globalTableColumn :loading.sync="listLoading" :key="key" :config="globalConfig" align="center" label="创建日期"
                         prop="createDate" width="170"></globalTableColumn>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button v-if="businessManager_btn_upd" size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="businessManager_btn_del" size="mini" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total"
      ></el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .filter-item {
    margin-left: 10px;
  }
</style>

<script>
  import {
    fetchList,
    getObj,
    addObj,
    putObj,
    delObj,
    casObj
  } from '@/api/store/business'
  // import { remoteData as userRemoteData } from '@/api/user'
  import waves from '@/directive/waves/index.js' // 水波纹指令
  import { mapGetters } from 'vuex'
  import { globalNotify } from '@/util/notifyUtils' //弹窗
  import { getToken } from '@/util/auth'
  import globalSearchTemplate from '@/components/global-el-search-template'
  import globalTableColumn from '@/components/global-el-table-column'
  import globalFormItem from '@/components/global-el-form-item'
  import { remoteData as userRemoteData } from '@/api/user/usersInfo'

  export default {
    name: 'business',
    components: { globalSearchTemplate, globalTableColumn, globalFormItem },
    directives: {
      waves
    },
    data() {
      return {
        globalConfig: {
          module: ['store'],
          componentName: ['business']
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          startTime: undefined,
          endTime: undefined,
          bankAccount: undefined,
          bankName: undefined,
          email: undefined,
          idCard: undefined,
          idCardPositiveImage: undefined,
          idCardReverseImage:undefined,
          identificationNumber: undefined,
          legalPerson: undefined,
          licenseImage: undefined,
          licenseNumber: undefined,
          mobile: undefined,
          organizationCode: undefined,
          organizationImage: undefined,
          taxImage: undefined,
          boundUserId: undefined,
          businessType:undefined
        },
        rules: {},
        form: {
          bankAccount: undefined,
          bankName: undefined,
          email: undefined,
          idCard: undefined,
          idCardImage: undefined,
          identificationNumber: undefined,
          legalPerson: undefined,
          licenseImage: undefined,
          licenseNumber: undefined,
          mobile: undefined,
          organizationCode: undefined,
          organizationImage: undefined,
          taxImage: undefined,
          boundUserId: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        businessManager_btn_add: false,
        businessManager_btn_upd: false,
        businessManager_btn_del: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        casDataUrl: casObj,
        key: '',
        formKey: '',
        userRemoteDataUrl: userRemoteData
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    filters: {},
    created() {
      this.getList()
      this.businessManager_btn_add = this.permissions['businessManager_btn_add']
      this.businessManager_btn_upd = this.permissions['businessManager_btn_upd']
      this.businessManager_btn_del = this.permissions['businessManager_btn_del']
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.data.records
          this.refreshTable()
          this.total = response.data.data.total
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleDelete(row) {
        delObj(row.id).then(response => {
          this.dialogFormVisible = false
          this.getList()
          globalNotify(this, response)
        })
      },
      handleCreate() {
        // this.dialogStatus = "create";
        // this.dialogFormVisible = true;
        // this.resetForm();
        // this.refreshForm();
        this.$router.push({ path: '/store/business/add' })
      },
      refreshTable() {
        this.key = new Date().getTime()
      },
      refreshForm() {
        this.formKey = new Date().getTime()
      },
      handleUpdate(row) {
        this.$router.push({ path: '/store/business/add', query: { businessId: row.id } })
      },
      create(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form).then(response => {
              this.dialogFormVisible = false
              this.getList()
              globalNotify(this, response)
            })
          } else {
            return false
          }
        })
      },
      cancel(formName) {
        this.resetForm()
        this.dialogFormVisible = false
      },
      update(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false
            this.form.password = undefined
            putObj(this.form).then(response => {
              this.dialogFormVisible = false
              this.getList()
              globalNotify(this, response)
            })
          } else {
            return false
          }
        })
      },
      resetForm() {
        this.form = {
          bankAccount: undefined,
          bankName: undefined,
          email: undefined,
          idCard: undefined,
          idCardImage: undefined,
          identificationNumber: undefined,
          legalPerson: undefined,
          licenseImage: undefined,
          licenseNumber: undefined,
          mobile: undefined,
          organizationCode: undefined,
          organizationImage: undefined,
          taxImage: undefined,
          boundUserId: undefined
        }
      },
      handleSearch() {
        this.listQuery.page = 1
        this.getList()
      }
    }
  }
</script>
